package com.maodou.android.ui.page.account

import android.content.Intent
import android.net.Uri
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.KeyboardArrowLeft
import androidx.compose.material.icons.rounded.KeyboardArrowRight
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Divider
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.maodou.android.R
import com.maodou.android.ui.page.CategoryKey
import com.maodou.android.viewmodel.account.AccountUiState
import com.maodou.android.viewmodel.account.AccountViewModel
import com.maodou.core.navigation.DialogStyle
import com.maodou.core.navigation.GlobalUiBus
import com.maodou.core.navigation.LocalDialogHandle
import com.maodou.core.navigation.Navigator3
import com.maodou.core.ui.components.ActionBar
import com.maodou.core.ui.components.AppText
import com.maodou.core.ui.components.HSpacer
import com.maodou.core.ui.components.TitleAlignment
import com.maodou.core.ui.components.VSpacer
import com.maodou.core.utils.permission.PermissionUtils
import kotlinx.coroutines.flow.distinctUntilChanged

/**
 * @Author Xing
 * @CreateTime 2025年09月18日
 * @Description 导入账号
 */
@Composable
fun ImportAccountScreen(
    viewModel: AccountViewModel = hiltViewModel(),
) {
    val context = LocalContext.current
    var platform by remember { mutableStateOf("Facebook") }
    var accountType by remember { mutableStateOf("个人号") }
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()
    val navigationBarHeight = WindowInsets.navigationBars.asPaddingValues().calculateBottomPadding()
    val picker =
        rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri: Uri? ->
            uri?.let {
                try {
                    viewModel.importAccount(it)
                    context.contentResolver.takePersistableUriPermission(
                        it,
                        Intent.FLAG_GRANT_READ_URI_PERMISSION
                    )
                } catch (_: Exception) {
                }
            }
        }

    BackHandler {
        back(uiState)
    }

    val lifecycleOwner = LocalLifecycleOwner.current
    DisposableEffect(lifecycleOwner) {
        onDispose {
            viewModel.clearTableData()
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
    ) {
        ActionBar(
            title = "账号导入",
            modifier = Modifier.fillMaxWidth(),
            navigationIcon = Icons.AutoMirrored.Outlined.KeyboardArrowLeft,
            onNavigationClick = { back(uiState) },
            backgroundColor = Color.Transparent,
            contentColor = MaterialTheme.colorScheme.onSurface,
            titleAlignment = TitleAlignment.Center,
        )

        Box(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
        ) {
            if (uiState.header.isEmpty()) Column(
                modifier = Modifier
                    .verticalScroll(rememberScrollState())
                    .padding(horizontal = 16.dp)
            ) {

                Spacer(modifier = Modifier.height(24.dp))

                Box(
                    modifier = Modifier.fillMaxWidth(),
                    contentAlignment = Alignment.Center
                ) {
                    Image(
                        painter = painterResource(R.mipmap.ic_account_import),
                        contentDescription = null,
                        modifier = Modifier.size(128.dp)
                    )
                }
                Spacer(modifier = Modifier.height(8.dp))
                Button(
                    onClick = {
                        PermissionUtils.requestReadWritePermissions {
                            picker.launch(
                                arrayOf(
                                    "text/csv",
                                    "text/comma-separated-values",
                                    "application/csv",
                                    "text/plain",
                                    "text/txt",
                                    "application/octet-stream",
                                    "application/vnd.ms-excel",
                                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                                )
                            )
                        }
                    },
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.colorScheme.primary,
                        contentColor = Color.White
                    ),
                    modifier = Modifier
                        .align(Alignment.CenterHorizontally)
                        .height(36.dp)
                ) {
                    Text(
                        text = "选择文件",
                        textAlign = TextAlign.Center,
                        style = MaterialTheme.typography.labelLarge
                    )
                }

                Spacer(modifier = Modifier.height(24.dp))

                Text(
                    text = "导入说明",
                    style = MaterialTheme.typography.titleMedium,
                    color = MaterialTheme.colorScheme.onSurface
                )

                Spacer(modifier = Modifier.height(12.dp))

                Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
                    Text(
                        text = "1、 工具暂时支持 Excel、CSV、TXT 格式文件导入",
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant
                    )
                    Text(
                        text = "2、 文件格式第一行为账号表头，其余行为账号信息，一行一个账号信息",
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant
                    )
                    Text(
                        text = "3、 支持导入的账号格式（表头不分先后）：",
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant
                    )

                    Spacer(modifier = Modifier.height(8.dp))

                    Text(
                        text = buildAnnotatedString {
                            val example = "示例：昵称|账号|密码|手机号|2FA|邮箱|邮箱密码|IP|Cookie|MD5"
                            append(example)
                            example.forEachIndexed { index, c ->
                                if (c == '|') {
                                    addStyle(
                                        style = SpanStyle(
                                            color = Color.Red,
                                            fontWeight = FontWeight.Bold
                                        ),
                                        start = index,
                                        end = index + 1
                                    )
                                }
                            }
                        },
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.onSurfaceVariant
                    )
                }

                Spacer(modifier = Modifier.height(12.dp))

            } else {
                Column {

                    // 账号基本信息
                    AppText(
                        text = "账号基本信息",
                        style = MaterialTheme.typography.titleSmall,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        modifier = Modifier.padding(bottom = 8.dp, start = 12.dp)
                    )

                    Box(
                        modifier = Modifier.padding(horizontal = 12.dp)
                    ) {
                        Column(
                            modifier = Modifier
                                .fillMaxWidth()
                                .shadow(elevation = 1.dp, shape = RoundedCornerShape(16.dp))
                                .clip(RoundedCornerShape(16.dp))
                                .background(Color.White)
                        ) {
                            Row(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .clickable {
                                        GlobalUiBus.showPopup(
                                            style = DialogStyle.Bottom,
                                            dismissOnClickOutside = true,
                                            dismissOnBackPress = false,
                                            content = {
                                                val handle = LocalDialogHandle.current
                                                val items =
                                                    arrayListOf("Facebook", "Instagram")
                                                BottomWheelPicker(
                                                    title = "平台",
                                                    items = items,
                                                    initialIndex = items.indexOf(platform)
                                                        .takeIf { it >= 0 } ?: 0,
                                                    onConfirm = { index ->
                                                        platform = items[index]
                                                        handle?.close()
                                                    },
                                                    onCancel = {
                                                        handle?.close()
                                                    }
                                                )
                                            }
                                        )
                                    }
                                    .padding(horizontal = 16.dp, vertical = 14.dp),
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                AppText(
                                    text = "平台",
                                    style = MaterialTheme.typography.bodyMedium,
                                    color = MaterialTheme.colorScheme.onSurface
                                )
                                Spacer(modifier = Modifier.weight(1f))
                                AppText(
                                    text = platform,
                                    color = MaterialTheme.colorScheme.onSurfaceVariant
                                )
                                HSpacer(4.dp)
                                Icon(
                                    imageVector = Icons.Rounded.KeyboardArrowRight,
                                    contentDescription = null,
                                    tint = MaterialTheme.colorScheme.onSurfaceVariant
                                )
                            }
                            HorizontalDivider(
                                color = MaterialTheme.colorScheme.outlineVariant,
                                modifier = Modifier.padding(horizontal = 16.dp)
                            )
                            Row(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .clickable {
                                        GlobalUiBus.showPopup(
                                            style = DialogStyle.Bottom,
                                            dismissOnClickOutside = true,
                                            dismissOnBackPress = false,
                                            content = {
                                                val handle = LocalDialogHandle.current
                                                val items = arrayListOf("个人号", "企业号")
                                                BottomWheelPicker(
                                                    title = "类型",
                                                    items = items,
                                                    initialIndex = items.indexOf(accountType)
                                                        .takeIf { it >= 0 } ?: 0,
                                                    onConfirm = { index ->
                                                        accountType = items[index]
                                                        handle?.close()
                                                    },
                                                    onCancel = { handle?.close() },
                                                    bottomActionText = "去添加",
                                                    onBottomAction = {
                                                        Navigator3.navigate(CategoryKey)
                                                        handle?.close()
                                                    },
                                                    bottomNote = "注：平台提供自定义账号分类，方便使用者管理账号"
                                                )
                                            }
                                        )
                                    }
                                    .padding(horizontal = 16.dp, vertical = 14.dp),
                                verticalAlignment = Alignment.CenterVertically
                            ) {
                                AppText(
                                    text = "类型",
                                    style = MaterialTheme.typography.bodyMedium,
                                    color = MaterialTheme.colorScheme.onSurface
                                )
                                Spacer(modifier = Modifier.weight(1f))
                                AppText(
                                    text = accountType,
                                    color = MaterialTheme.colorScheme.onSurfaceVariant
                                )
                                HSpacer(4.dp)
                                Icon(
                                    imageVector = Icons.Rounded.KeyboardArrowRight,
                                    contentDescription = null,
                                    tint = MaterialTheme.colorScheme.onSurfaceVariant
                                )
                            }
                        }
                    }

                    VSpacer(16.dp)

                    AppText(
                        text = "账号信息${if(uiState.rows.isNotEmpty()) ":共(${uiState.rows.size})账号" else ""}",
                        style = MaterialTheme.typography.titleSmall,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        modifier = Modifier.padding(start = 12.dp)
                    )
                    Text(
                        text = "每个单元格均可编辑",
                        style = MaterialTheme.typography.bodySmall,
                        fontSize = 10.sp,
                        lineHeight = 12.sp,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        modifier = Modifier.padding(horizontal = 12.dp)
                    )
                    VSpacer(8.dp)
                    ImportPreviewTable(
                        modifier = Modifier
                            .weight(1f)
                            .padding(horizontal = 12.dp),
                        header = uiState.header,
                        rows = uiState.rows,
                        onSaveRow = { index, newValues ->
                            viewModel.updateRow(index, newValues)
                        },
                        onDeleteRow = { index ->
                            viewModel.removeRow(index)
                        }
                    )
                    Spacer(modifier = Modifier.height(16.dp))

                    Box(
                        modifier = Modifier
                            .shadow(2.dp)
                            .background(Color.White)
                            .padding(
                                start = 32.dp,
                                end = 32.dp,
                                top = 16.dp,
                                bottom = 16.dp + navigationBarHeight
                            ),
                        contentAlignment = Alignment.Center
                    ) {
                        Button(
                            onClick = { viewModel.saveImportAccount(platform,accountType) },
                            enabled = !uiState.isSave,
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(48.dp),
                            shape = RoundedCornerShape(12.dp)
                        ) {
                            if (uiState.isSave) {
                                CircularProgressIndicator(
                                    modifier = Modifier.size(16.dp),
                                    strokeWidth = 2.dp,
                                    color = MaterialTheme.colorScheme.onSecondary
                                )
                                HSpacer(4.dp)
                                Text(
                                    text = "保存中...",
                                    textAlign = TextAlign.Center,
                                    color = MaterialTheme.colorScheme.onSecondary
                                )
                            } else Text(text = "保存", textAlign = TextAlign.Center)
                        }
                    }
                }
            }
        }
    }

}

@Composable
private fun ImportPreviewTable(
    header: List<String>,
    rows: List<List<String>>,
    onSaveRow: (Int, List<String>) -> Unit,
    onDeleteRow: (Int) -> Unit,
    modifier: Modifier = Modifier
) {
    val actionColWidth = 80.dp
    val cellWidth: Dp = 160.dp
    val headerHeight = 44.dp
    val rowHeight = 48.dp
    val corner = RoundedCornerShape(10.dp)

    var editableRows by remember(header, rows) {
        mutableStateOf(rows.map { it.toMutableList() }.toMutableList())
    }

    val hScroll = rememberScrollState()
    val leftListState = rememberLazyListState()
    val rightListState = rememberLazyListState()
    val tableWidth = cellWidth * header.size

    LaunchedEffect(leftListState, rightListState) {
        snapshotFlow { leftListState.firstVisibleItemIndex to leftListState.firstVisibleItemScrollOffset }
            .distinctUntilChanged()
            .collect { (index, offset) ->
                if (leftListState.isScrollInProgress &&
                    (rightListState.firstVisibleItemIndex != index || rightListState.firstVisibleItemScrollOffset != offset)
                ) {
                    rightListState.scrollToItem(index, offset)
                }
            }
    }
    LaunchedEffect(rightListState, leftListState) {
        snapshotFlow { rightListState.firstVisibleItemIndex to rightListState.firstVisibleItemScrollOffset }
            .distinctUntilChanged()
            .collect { (index, offset) ->
                if (rightListState.isScrollInProgress &&
                    (leftListState.firstVisibleItemIndex != index || leftListState.firstVisibleItemScrollOffset != offset)
                ) {
                    leftListState.scrollToItem(index, offset)
                }
            }
    }

    Column(
        modifier = modifier
            .fillMaxWidth()
            .clip(corner)
            .border(1.dp, MaterialTheme.colorScheme.outlineVariant, corner)
            .background(Color.White)
            .padding(0.dp)
    ) {

        // 表头
        Box(
            Modifier
                .fillMaxWidth()
                .height(headerHeight)
                .background(MaterialTheme.colorScheme.surface)
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(end = actionColWidth)
                    .horizontalScroll(hScroll)
                    .height(headerHeight),
                verticalAlignment = Alignment.CenterVertically
            ) {
                header.forEach { title ->
                    TableHeaderCell(text = title, width = cellWidth, height = headerHeight)
                }
            }

            Box(
                modifier = Modifier
                    .align(Alignment.TopEnd)
                    .width(actionColWidth)
                    .fillMaxHeight()
                    .background(MaterialTheme.colorScheme.surfaceVariant),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = "操作",
                    style = MaterialTheme.typography.labelLarge,
                    color = MaterialTheme.colorScheme.onSurface
                )
            }
        }

        // 数据行（左右两个 LazyColumn 同步纵向滚动）
        Box(Modifier.fillMaxWidth().heightIn(min = 120.dp)) {
            // 左侧数据表：将横向滚动上提到容器，避免每行 horizontalScroll 抢占纵向手势
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(end = actionColWidth)
                    .horizontalScroll(hScroll)
            ) {
                LazyColumn(
                    state = leftListState,
                    modifier = Modifier
                        .width(tableWidth)
                        .fillMaxHeight()
                        .clipToBounds()
                ) {
                    itemsIndexed(editableRows, key = { idx, _ -> idx }) { rowIndex, rowValues ->
                        val rowBgColor =
                            if (rowIndex % 2 == 0) MaterialTheme.colorScheme.surface else MaterialTheme.colorScheme.surfaceVariant.copy(
                                alpha = 0.25f
                            )
                        Row(
                            modifier = Modifier
                                .height(rowHeight)
                                .fillMaxWidth()
                                .background(rowBgColor),
                            verticalAlignment = Alignment.CenterVertically,
                            horizontalArrangement = Arrangement.Center
                        ) {
                            val colCount = header.size
                            for (c in 0 until colCount) {
                                val value = rowValues.getOrNull(c) ?: ""
                                EditableTableCell(
                                    value = value,
                                    width = cellWidth,
                                    height = rowHeight,
                                    onValueChange = { new ->
                                        val copy = editableRows.toMutableList()
                                        val rowCopy = copy[rowIndex].toMutableList()
                                        // 扩展到对应列
                                        while (rowCopy.size <= c) rowCopy.add("")
                                        rowCopy[c] = new
                                        copy[rowIndex] = rowCopy
                                        editableRows = copy
                                    }
                                )
                                if (c != colCount - 1) {
                                    Box(
                                        Modifier
                                            .width(1.dp)
                                            .fillMaxHeight()
                                            .background(MaterialTheme.colorScheme.outlineVariant)
                                    )
                                }
                            }
                        }
                        Divider(color = MaterialTheme.colorScheme.outlineVariant, thickness = 1.dp)
                    }
                }
            }
            // 操作栏左侧边界的阴影（横向渐变，增强层次感）
            Box(
                modifier = Modifier
                    .align(Alignment.TopEnd)
                    .offset(x = -actionColWidth)
                    .width(8.dp)
                    .fillMaxHeight()
                    .background(
                        brush = Brush.horizontalGradient(
                            colors = listOf(
                                Color.Transparent,
                                MaterialTheme.colorScheme.onSurface.copy(alpha = 0.14f)
                            )
                        )
                    )
            )

            // 右侧固定操作栏（与左侧同步纵向滚动）
            LazyColumn(
                state = rightListState,
                modifier = Modifier
                    .align(Alignment.TopEnd)
                    .width(actionColWidth)
                    .fillMaxHeight()
                    .background(MaterialTheme.colorScheme.surface)
            ) {
                itemsIndexed(editableRows, key = { idx, _ -> idx }) { index, _ ->
                    val rowBgColor =
                        if (index % 2 == 0) MaterialTheme.colorScheme.surface else MaterialTheme.colorScheme.surfaceVariant.copy(
                            alpha = 0.25f
                        )
                    Row(
                        modifier = Modifier
                            .height(rowHeight)
                            .fillMaxWidth()
                            .background(rowBgColor)
                            .padding(horizontal = 8.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Button(
                            onClick = { onSaveRow(index, editableRows[index].toList()) },
                            modifier = Modifier
                                .height(32.dp)
                                .weight(1f),
                            contentPadding = PaddingValues(horizontal = 0.dp),
                            colors = ButtonDefaults.buttonColors(
                                containerColor = Color.Transparent,
                                contentColor = Color.Transparent
                            )
                        ) {
                            Text("保存", style = MaterialTheme.typography.labelMedium,fontSize = 12.sp, color = MaterialTheme.colorScheme.primary)
                        }
                        Spacer(Modifier.width(8.dp))
                        Button(
                            onClick = {
                                // 本地先删除再上抛
                                val copy = editableRows.toMutableList()
                                if (index in copy.indices) copy.removeAt(index)
                                editableRows = copy
                                onDeleteRow(index)
                            },
                            modifier = Modifier
                                .height(32.dp)
                                .weight(1f),
                            contentPadding = PaddingValues(horizontal = 0.dp),
                            colors = ButtonDefaults.buttonColors(
                                containerColor = Color.Transparent,
                                contentColor = Color.Transparent
                            )
                        ) {
                            Text("删除", style = MaterialTheme.typography.labelMedium, fontSize = 12.sp, color = MaterialTheme.colorScheme.error)
                        }
                    }
                    Divider(color = MaterialTheme.colorScheme.outlineVariant, thickness = 1.dp)
                }
            }
        }
    }
}

@Composable
private fun TableHeaderCell(text: String, width: Dp, height: Dp) {
    Box(
        modifier = Modifier
            .width(width)
            .height(height)
            .border(0.5.dp, MaterialTheme.colorScheme.outlineVariant)
            .background(MaterialTheme.colorScheme.surfaceVariant),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = text.ifBlank { "(空)" },
            style = MaterialTheme.typography.labelLarge,
            color = MaterialTheme.colorScheme.onSurface
        )
    }
}

@Composable
private fun EditableTableCell(
    value: String,
    width: Dp,
    height: Dp,
    onValueChange: (String) -> Unit,
) {
    Box(
        modifier = Modifier
            .width(width)
            .height(height - 8.dp)
            .padding(horizontal = 4.dp, vertical = 4.dp)
            .clip(RoundedCornerShape(6.dp)),
        contentAlignment = Alignment.Center
    ) {
        BasicTextField(
            value = value,
            onValueChange = onValueChange,
            singleLine = true,
            textStyle = MaterialTheme.typography.bodySmall.copy(
                color = MaterialTheme.colorScheme.onSurface,
                textAlign = TextAlign.Center
            ),
            modifier = Modifier
                .fillMaxSize()
                .padding(horizontal = 8.dp, vertical = 8.dp),
            decorationBox = { innerTextField ->
                Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                    innerTextField()
                }
            }
        )
    }
}

fun back(uiState: AccountUiState){
    if(uiState.rows.isEmpty()) Navigator3.pop()
    else {
        GlobalUiBus.showAlertDialog(
            title = "退出后表格数据将被清空！是否继续",
            positiveText = "确认退出",
            negativeText = "取消",
            cancelable = true,
            onConfirm = {
                Navigator3.pop()
            }
        )
    }
}

@Preview(showBackground = true)
@Composable
fun ImportAccountScreenP() {
    ImportAccountScreen()
}